/**
 * Created by Administrator on 2017/2/6.
 */


const http = require('http')
const iconv = require('iconv-lite');
const cheerio = require('cheerio');
const url = require('url');

const fs = require('fs');

const filesys = require('./filesys')

const connection = require('./mysql').connection;


exports.spider = function (list) {

    for(var i = 0, len = list.length; i < 1; i++){

        getPageList(list[i].href);
       /* get(list[i].href).then((str) => {
            var $ = cheerio.load(str);
            var items =[];
            $('#con_xin_1 ul a').each(function (i,element) {
                var $element = $(element);
                items.push({
                    index: i,
                    title: $element.attr('title'),
                    href: $element.attr('href'),
                    img: $element.find('img').attr('src'),
                    date: $element.parent().children('span').text().split('：')[1],
                });

            });
            console.log(items);

            $('.list_page strong').next().attr('href')


        });*/

    }
}
// getPageContent({href:'http://www.laoguo5.com/kangwangjiadao/75801.html'},'http://www.laoguo5.com/kangwangjiadao/75806.html')
 getPageList('http://www.laoguo5.com/zuixin/');

function getPageContent(item, href) {
    //console.log(item);


    get(url.resolve(href,item.href)).then((str) => {
        var $ = cheerio.load(str);

         // console.log(str)
          // try{


            let videostr = str.match(/videostr = \'(.+)\'/) == null ? '' :str.match(/videostr = \'(.+)\'/)[1];
            let c_pic = str.match(/c_pic   = \'(.+)\'/) == null ? null :str.match(/c_pic   = \'(.+)\'/)[1];
            let mp3_url  = str.match(/mp3_url = \'(.+)\'/) == null ? null : str.match(/mp3_url = \'(.+)\'/)[1];

            let title = $('title').text().replace('- 老和360','');
            let description = $('[name="description"]').attr('content');

            Object.assign(item,{
                videostr: videostr,
                c_pic: c_pic,
                mp3_url: mp3_url,
                title2: title,
                description: description
            });
          // }catch(e){
          //         console.log(str.match(/videostr = \'(.+)\'/) , item)
          // }
         //console.log(item)

        get(url.resolve(href, item.img),false).then((buf) => {
            let filePath = 'E:\\img'+item.img.replace(/\//g,'\\');
            let fileFolder = filePath.substring(0,filePath.lastIndexOf('\\'));
            //console.log(filePath,'====',fileFolder)
            if(!fs.existsSync(fileFolder))
                filesys.mkdirsSync(fileFolder, 777);
            fs.open(filePath, 'w', (err, fd) => {
                if (err) throw err;
                fs.writeFile(filePath, buf, (err) => {
                    if (err) throw err;
                    console.log(item.img + ' saved!');
                })
            })
        });

        connection.query('insert into xiaopin(title,img,video,mp3_url,sub_title,description,date)values(?,?,?,?,?,?,?)'
            ,[item.title,item.img,item.videostr,item.mp3_url,item.title2,item.description,item.date]
            , (err, result,fields) => {

                if (err) throw err;
            });


        /*fs.appendFile('data.json', JSON.stringify(item)+',', (err) => {
            if (err) throw err;
            console.log(item.href +' was appended to file!');
        });*/


    },(err) =>{
        console.log(err)
    }).catch((e) => {
        console.log(e);
    });
}

function getPageList(href) {
    console.log(href);
    get(href).then((str) => {
        var $ = cheerio.load(str);
        var items =[];
        $('#con_xin_1 ul a').each(function (i,element) {
            var $element = $(element);
            items.push({
                index: i,
                title: $element.attr('title'),
                href: $element.attr('href'),
                img: $element.find('img').attr('src'),
                date: $element.parent().children('span').text().split('：')[1],
            });

        });
       // console.log(items);

        for(i in items){
            getPageContent(items[i],href);
        }

        if($('.list_page strong').next().attr('href')){
           //setTimeout(() =>{

               getPageList(url.resolve(href,$('.list_page strong').next().attr('href')));
           //},1000) ;
        }

    },(err) =>{
        console.log(err)
    });
}

function get(href, decode) {
    if(decode === undefined) decode = true;
    return new Promise((resolve , reject) => {
        var req = http.get( {
            hostname: '127.0.0.1',
            port: 8087,
            path: href,
            headers: {
                host: url.parse(href).hostname
            }
        } //*/   href

        , (res) => {

            if(res.statusCode != 200){
                reject(res.statusCode);
                return;
            }

            var chunks = []
            res.on('data', function (chunk) {
                chunks.push(chunk);
            });

            res.on('end', function () {
                var buf = Buffer.concat(chunks);
                if(decode)
                    buf = iconv.decode(buf, 'gbk');

                resolve(buf);

            });
        });
        req.on('error', function (err) {
            reject(err)
        })
    })

}

exports.get = get;